package com.brillilab.starter.controller.web.notice;

import com.brillilab.common.entity.PageVo;
import com.brillilab.common.entity.ResponseVo;
import com.brillilab.domain.enums.lab.LabMemberPowerEnum;
import com.brillilab.domain.pa.notice.NoticePa;
import com.brillilab.domain.pa.notice.NoticeSearchPa;
import com.brillilab.domain.po.notice.Notice;
import com.brillilab.domain.vo.lab.LabMemberVo;
import com.brillilab.domain.vo.notice.NoticeDetail;
import com.brillilab.domain.vo.notice.NoticeSearch;
import com.brillilab.domain.vo.notice.NoticeVo;
import com.brillilab.domain.vo.user.UserInfoVo;
import com.brillilab.service.core.notice.INoticeService;
import com.brillilab.service.logic.user.UsersLogic;
import com.brillilab.starter.aspect.AspectLog;
import com.brillilab.starter.constant.StarterConstant;
import com.brillilab.starter.handler.RequestPower;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 公告
 * @author wmh
 */
@RestController(value="WebNoticeController")
@RequestMapping("/web/notice")
public class NoticeController {

    @Resource
    private INoticeService noticeService;

    @Resource
    private UsersLogic usersLogic;

    /**
     * 公告列表(排序/搜索)
     * @return
     */
    @RequestMapping(value="/list",method=RequestMethod.POST)
    public ResponseVo getNoticeList(@RequestBody NoticeSearchPa noticeSearchPa,@RequestHeader(value=StarterConstant.TOKEN) String token){

        Assert.isTrue(noticeSearchPa!=null && noticeSearchPa.getPageNum()!=null && noticeSearchPa.getPageSize()!=null,"请求参数缺失！");

        UserInfoVo userInfo=usersLogic.getUserByToken(token);

        Assert.notNull(userInfo.getLabId(),"用户未加入实验室！");

        noticeSearchPa.setLabId(userInfo.getLabId());

        NoticeSearch noticeSearch=new NoticeSearch();
        BeanUtils.copyProperties(noticeSearchPa,noticeSearch);

        PageVo<NoticeVo> page=noticeService.getAndSearchNoticeList(noticeSearch);

        return ResponseVo.success(page);
    }

    /**
     * 公告详情
     * @param noticeId
     * @param token
     * @return
     */
    @RequestMapping(value="/detail",method=RequestMethod.GET)
    public ResponseVo getNoticeDetail(Long noticeId,@RequestHeader(value=StarterConstant.TOKEN) String token){

        Assert.notNull(noticeId,"noticeId不能为空");

        UserInfoVo userInfo=usersLogic.getUserByToken(token);

        Assert.notNull(userInfo.getLabId(),"用户未加入实验室！");

        NoticeDetail noticeDetail=noticeService.getNoticeDetailAndSetReadMember(noticeId,userInfo);

        Assert.notNull(noticeDetail,"通知不存在！");

        return ResponseVo.success(noticeDetail);
    }

    /**
     * 公告已读成员
     * @param noticeId
     * @param token
     * @return
     */
    @RequestMapping(value="/read/member",method=RequestMethod.GET)
    public ResponseVo getReadMembers(Long noticeId,@RequestHeader(value=StarterConstant.TOKEN) String token){

        Map<String,Object> resultMap = new HashMap<>();

        Assert.notNull(noticeId,"请求参数缺失！");

        UserInfoVo userInfo=usersLogic.getUserByToken(token);

        Assert.notNull(userInfo.getLabId(),"用户未加入实验室！");

        List<LabMemberVo> labMemberVos = noticeService.selectReadMemberList(noticeId,userInfo.getLabId());

        resultMap.put("labMembers",labMemberVos);
        resultMap.put("num",labMemberVos.size());

        return ResponseVo.success(resultMap);
    }

    /**
     * 发布公告
     * @param notice
     * @param token
     * @return
     */
    @AspectLog
    @RequestMapping(value="/publish",method=RequestMethod.POST)
    @RequestPower(LabMemberPowerEnum.notice)
    public ResponseVo publishNotice(@RequestBody NoticePa notice ,@RequestHeader(value=StarterConstant.TOKEN) String token){

        Assert.isTrue(notice!=null && StringUtils.isNotBlank(notice.getDescription()),"请求参数缺失！");

        UserInfoVo userInfo=usersLogic.getUserByToken(token);

        Assert.notNull(userInfo.getLabId(),"用户未加入实验室！");

        Notice insert=noticeService.publishNotice(notice,userInfo);

        return ResponseVo.success(insert);
    }

    /**
     * 编辑公告
     * @param notice
     * @param token
     * @return
     */
    @AspectLog
    @RequestMapping(value="/edit",method=RequestMethod.POST)
    @RequestPower(LabMemberPowerEnum.notice)
    public ResponseVo editeNotice(@RequestBody NoticePa notice,@RequestHeader(value=StarterConstant.TOKEN) String token){

        Assert.isTrue(notice!=null && notice.getId()!=null &&!StringUtils.isEmpty(notice.getDescription()),"请求参数缺失！");

        UserInfoVo userInfo=usersLogic.getUserByToken(token);

        Assert.notNull(userInfo.getLabId(),"用户未加入实验室！");

        Notice update=noticeService.editeNotice(notice,userInfo);

        return ResponseVo.success(update);
    }

    /**
     * 删除公告
     * @param noticeId
     * @param token
     * @return
     */
    @AspectLog
    @RequestMapping(value="/delete",method=RequestMethod.GET)
    @RequestPower(LabMemberPowerEnum.notice)
    public ResponseVo deleteNotice(Long noticeId,@RequestHeader(value=StarterConstant.TOKEN) String token){

        Assert.notNull(noticeId,"请求参数缺失！");

        UserInfoVo userInfo=usersLogic.getUserByToken(token);

        Assert.notNull(userInfo.getLabId(),"用户未加入实验室！");

        Boolean rs=noticeService.delete(noticeId,userInfo.getLabId());

        Assert.isTrue(rs,"业务操作失败！");

        return ResponseVo.success();
    }

    /**
     * 公告 顶置/取消顶置
     * @param noticeId
     * @param isTop
     * @param token
     * @return
     */
    @RequestMapping(value="/top",method=RequestMethod.GET)
    @RequestPower(LabMemberPowerEnum.notice)
    public ResponseVo setTopNotice(Long noticeId,Integer isTop,@RequestHeader(value=StarterConstant.TOKEN) String token){

        Assert.isTrue(noticeId!=null && isTop!=null,"请求参数缺失！");

        UserInfoVo userInfo=usersLogic.getUserByToken(token);

        Assert.notNull(userInfo.getLabId(),"用户未加入实验室！");

        return ResponseVo.success(noticeService.setTopNotice(noticeId,isTop,userInfo));
    }

}
